package spring.validation;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.lang.reflect.Field;
import java.util.StringJoiner;

/**
 * 通用实体类
 *
 * @author ME
 * @date   2019/10/21
 */
public class Person {

    @BytesLength(max = 3)
    private String name;

    @Min(0)
    @Max(10)
    private String age;

    @NotBlank
    private String testNotBlank;

    @Size(max = 3)
    private String testSize;

    @Length(max = 3, message = "长度不能超过 {max} 个字符")
    private String testLength;

    @Pattern(regexp = "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}")
    private String time;

    private List<String> hobby;

    public Person() {
        super();
    }

    public Person(String name) {
        super();
        this.name = name;
    }

    public Person(String name, String age) {
        super();
        this.name = name;
        this.age = age;
    }

    public static class PersonBuilder {
        private String[] columns;
        private String[] values;

        public PersonBuilder(String columnStr) {
            this.columns = columnStr.split("\\|");
        }

        public PersonBuilder setValues(String value) {
            this.values = value.split("\\|", -1);
            return this;
        }

        public Person builder() throws NoSuchFieldException, IllegalAccessException {
            Person person = new Person();
            for (int i = 0; i < columns.length; i++) {
                Field field = person.getClass().getDeclaredField(columns[i]);
                field.setAccessible(true);
                field.set(person, values[i]);
            }
            return person;
        }
    }

    public void addHobby(String hobby) {
        this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());
        this.hobby.add(hobby);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getTestNotBlank() {
        return testNotBlank;
    }

    public void setTestNotBlank(String testNotBlank) {
        this.testNotBlank = testNotBlank;
    }

    public String getTestSize() {
        return testSize;
    }

    public void setTestSize(String testSize) {
        this.testSize = testSize;
    }

    public String getTestLength() {
        return testLength;
    }

    public void setTestLength(String testLength) {
        this.testLength = testLength;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public List<String> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        StringJoiner joiner = new StringJoiner(", ", Person.class.getSimpleName() + "[", "]")
                .add("name='" + name + "'")
                .add("age='" + age + "'");
        if (testSize != null) {
            joiner.add("testSize='" + testSize + "'");
        }
        if (testLength != null) {
            joiner.add("testSize='" + testLength + "'");
        }
        if (time != null) {
            joiner.add("testSize='" + time + "'");
        }
        if (testSize != null) {
            joiner.add("hobby=" + hobby);
        }

        return joiner.toString();
    }

}
